package com.qf.service.impl;

import com.qf.dao.GoodsDetailsMapper;
import com.qf.dao.jpa.GoodsDetailsJpa;
import com.qf.entity.GoodsDetails;
import com.qf.entity.GoodsDetailsPage;
import com.qf.service.GoodsDetailsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class GoodsDetailsServiceImpl implements GoodsDetailsService {

    @Resource
    private GoodsDetailsMapper goodsDetailsMapper;

    @Resource
    private GoodsDetailsJpa goodsDetailsJpa;

    @Override
    public List<GoodsDetails> findAllGoodDetails() {
        return goodsDetailsMapper.findAllGoodDetails();
    }

    @Override
    public GoodsDetailsPage getGoodsDetailsPage(Integer currentpage, Integer pagesize) {
        PageRequest request = new PageRequest(currentpage-1,pagesize);
        Page<GoodsDetails> all = goodsDetailsJpa.findAll(request);
        List<GoodsDetails> details = all.getContent();
        long total = all.getTotalElements();

        GoodsDetailsPage goodsDetailsPage=new GoodsDetailsPage();
        goodsDetailsPage.setTotal(total);
        goodsDetailsPage.setList(details);
        return goodsDetailsPage;
    }

    @Override
    public int deleteGoodsByGdid(Integer gdid) {
        return goodsDetailsMapper.deleteGoodsByGdid(gdid);
    }

    @Override
    public void updateGoodsByGdid(GoodsDetails goodsDetails) {
        goodsDetailsJpa.saveAndFlush(goodsDetails);
    }

    @Override
    public void saveGoodsDetails(GoodsDetails goodsDetails) {
        goodsDetailsJpa.save(goodsDetails);
    }

    @Override
    public GoodsDetails getGoodsDetailByGdid(Integer gdid) {
        Optional<GoodsDetails> goodsDetails = goodsDetailsJpa.findById(gdid);
        GoodsDetails goodsDetails1 = goodsDetails.get();
        return goodsDetails1;
    }

    @Override
    public List<String> findByGdnameGoods() {
        return goodsDetailsMapper.findByGdnameGoods();
    }

    @Override
    public int getGoodDetailGdidByGdname(String gdname) {
        return goodsDetailsMapper.getGoodDetailGdidByGdname(gdname);
    }

}
